package org.aicfve.global.snapshot.controller;

import com.hava.web.MediaType;
import com.hava.web.rest.Entity;
import org.aicfve.config.StorageConfig;
import org.aicfve.global.snapshot.service.FileService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value="test/file")
public class FileSubmitController {

    @Resource
    private StorageConfig storageConfig;
    @Autowired
    SimpleDateFormat saveFormat;
    @Autowired
    SimpleDateFormat dayFormat;

    @Autowired
    FileService fileService;

    @RequestMapping(value = "/onlysubmit", method = RequestMethod.POST)
    public String uploadTest(@RequestParam("file")CommonsMultipartFile file) throws IOException {
        // done: file storage where
        Date now = new Date();
        String save_path= storageConfig.getSavepath() + dayFormat.format(now) + "/" + saveFormat.format(now) + "_"+ file.getOriginalFilename();
        System.out.println("[save_path]:" + save_path);
        // save file
        File saveFile = new File(save_path);
        // create parent dir
        FileUtils.forceMkdirParent(saveFile);
        System.out.println("[file.path]:" + saveFile.getPath());
        file.transferTo(saveFile);

        return "success";
    }

    @RequestMapping(method = RequestMethod.GET, produces = MediaType.JSON)
    public List<org.aicfve.global.snapshot.entity.File> getAll(){
        return fileService.getAll();
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET,produces = MediaType.JSON)
    public org.aicfve.global.snapshot.entity.File getOne(@PathVariable("id") Long id){
        return fileService.get(id);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity uploadPreview(@RequestParam("file")CommonsMultipartFile multipartFile, UriComponentsBuilder uriBuilder) throws Exception {

        HttpHeaders httpheader = new HttpHeaders();
        ResponseEntity responseEntity;

        // done: file storage where
        Date now = new Date();
        String save_path= storageConfig.getSavepath() + dayFormat.format(now) + "/" + saveFormat.format(now) + "_"+ multipartFile.getOriginalFilename();
        System.out.println("[save_path]:" + save_path);

        // save file
        String normalized = FilenameUtils.normalize(save_path);
        File saveFile = new File(normalized);
        // 创建这个文件的parent目录
        FileUtils.forceMkdirParent(saveFile);
        System.out.println("[file.path]:" + saveFile.getPath());
        multipartFile.transferTo(saveFile);

        // create new entity
        org.aicfve.global.snapshot.entity.File entity = new org.aicfve.global.snapshot.entity.File();

        //multipartFile.getFileItem(); ??
        entity.setOriginalFilename(multipartFile.getOriginalFilename());
        entity.setName(multipartFile.getName());
        entity.setContentType(multipartFile.getContentType());
        entity.setSize(multipartFile.getSize());
        entity.setSavepath(save_path);

        String url_path = storageConfig.getUrlpath() + dayFormat.format(now) + "/" + saveFormat.format(now) + "_"+ multipartFile.getOriginalFilename();
        System.out.println("[url_path]:" + url_path);
        entity.setUrlpath(url_path);

        fileService.create(entity);

        // todo: return new version url
        //通过反射获取entity的url
        String EntityURL = Entity.URL(this) + "/" + entity.getId();//snapshot.getVersion();
        //生成Respone header显示Entity创建后的资源路径
        URI uri = uriBuilder.path(EntityURL).build().toUri();
        httpheader.setLocation(uri);
        responseEntity = new ResponseEntity(httpheader, HttpStatus.CREATED);

        return responseEntity;
    }
}
